package me.zhengjie.modules.other.rest;

import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.service.member.service.DepartmentService;
import com.qiaofang.applet.service.other.domain.CityCompany;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.other.service.dto.CityCodeDto;
import com.qiaofang.applet.service.other.service.dto.CityCompanyDto;
import com.qiaofang.applet.service.other.service.mapstruct.CityCompanyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by IDEA.
 *
 * @auther Zym.
 * @company qiaofang
 * @date 2020/9/15 15:32
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "内容：公司管理")
@RequestMapping("/api/company")
public class CompanyController {
    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private CityCompanyMapper cityCompanyMapper;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private UserService userService;

    @Log("保存公司")
    @ApiOperation("保存公司")
    @PostMapping(value = "/save")
    public ResponseEntity<Object> save(@RequestBody DataRequest<CityCompanyDto> request) {
        cityCompanyService.saveCompany(cityCompanyMapper.toEntity(request.getParam()));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("移除公司")
    @ApiOperation("移除公司")
    @PostMapping(value = "/remove")
    public ResponseEntity<Object> remove(@RequestBody DataRequest<String> request) {
        cityCompanyService.deleteById(request.getParam());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("公司列表")
    @ApiOperation("公司列表")
    @PostMapping(value = "/list")
    public ResponseEntity<Page<CityCompanyDto>> list(@RequestBody PageDataRequest<String> request) {
        CityCompany cityCompany = new CityCompany();
        cityCompany.setCorporationCode(request.getParam());
        Page<CityCompany> result = cityCompanyService.findAll(Example.of(cityCompany), PageUtil.getPageable(request));
        return ResponseEntity.ok(result.map(cityCompanyMapper::toDto));
    }

    @ApiOperation("获取城市列表")
    @PostMapping(value = "/getCityList")
    public ResponseEntity<Object> getCityList(){
        String corporationCode = SecurityUtils.getCorporationCode();
        List<String> cityCodeList = userService.getCityCodeList(SecurityUtils.getCurrentUsername());
        if (!CollectionUtils.isEmpty(cityCodeList)) {
            return ResponseEntity.ok(cityCompanyService.getCityList(corporationCode, cityCodeList));
        }
        return ResponseEntity.ok(cityCompanyService.getCityList(corporationCode));
    }

    @ApiOperation("获取城市列表(无限制)")
    @PostMapping(value = "/getCityListUnlimited")
    public ResponseEntity<Object> getCityListUnlimited(){
        String corporationCode = SecurityUtils.getCorporationCode();
        return ResponseEntity.ok(cityCompanyService.getCityList(corporationCode));
    }

    @ApiOperation("获取部门列表")
    @PostMapping(value = "/getDeptList")
    public ResponseEntity<Object> getDeptList(@RequestBody DataRequest<CityCodeDto> request){
        String corporationCode = SecurityUtils.getCorporationCode();
        return ResponseEntity.ok(departmentService.getDeptList(corporationCode, request.getParam().getCityCode()));
    }
}
